package com.coszero.common.base;


import android.annotation.SuppressLint;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.coszero.ui.dialog.ToastUtils;
import com.coszero.utils.app.AppManager;
import com.coszero.utils.app.AppUtils;
import com.coszero.utils.base.BaseRecyclerAdapter;
import com.coszero.utils.base.refresh.AdapterRefreshInterface;
import com.coszero.utils.utils.LogX;
import com.coszero.common.config.Constant;
import com.coszero.commonlib.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Desc：
 *
 * @author xmqian
 * Email:xmqian93@163.com
 * Date: 2021/8/20 17:02
 * @version 1
 */
@Deprecated
@SuppressLint("ObsoleteSdkInt")
@SuppressWarnings("deprecation,unchecked")
public class AdapterRefresh<T> implements AdapterRefreshInterface<T> {
    private boolean isLoadMore;
    private boolean isRefresh;
    private int rows;
    private SmartRefreshLayout moreRecyclerView;
    private RecyclerView.Adapter adapter;
    private int imageRes = 0;//空布局图标
    private String emptyStr = "";//空布局消息
    private int firstPageCode = 1;

    /**
     * @param isRefresh 下拉刷新
     * @param isLoadMore 加载更多
     * @param rows
     */
    public AdapterRefresh(boolean isRefresh, boolean isLoadMore, int rows) {
        this.isLoadMore = isLoadMore;
        this.isRefresh = isRefresh;
        this.rows = rows;
    }

    public AdapterRefresh(boolean isRefresh, boolean isLoadMore) {
        this(isRefresh, isLoadMore, Constant.ROWS);
    }

    public void init(SmartRefreshLayout moreRecyclerView, RecyclerView.Adapter adapterBase) {
        this.moreRecyclerView = moreRecyclerView;
        this.adapter = adapterBase;
        moreRecyclerView.setEnableRefresh(isRefresh);
        moreRecyclerView.setEnableLoadMore(isLoadMore);
    }

    /**
     * @param firstPageCode 设置首页页码，默认为0
     */
    public void setFirstPageCode(int firstPageCode) {
        this.firstPageCode = firstPageCode;
    }

    @Override
    @SuppressWarnings({"unchecked", "deprecation"})
    public void doRefresh(List<T> list, int page) {
        if (null != list) {
            if (list.size() >= rows && isLoadMore) {
                moreRecyclerView.setEnableLoadMore(true);
            } else {
                moreRecyclerView.setEnableLoadMore(false);
            }
            if (page <= firstPageCode) {
                if (list.size() == 0) {
//                    AppUtil.showToast(R.string.toast_nodata);
                }
                if (adapter instanceof BaseQuickAdapter) {
                    ((BaseQuickAdapter) adapter).setNewInstance(list);
                }
                if (adapter instanceof BaseRecyclerAdapter) {
                    ((BaseRecyclerAdapter) adapter).refresh(list);
                }
            } else {
                if (list.size() > 0) {
                    if (adapter instanceof BaseQuickAdapter) {
                        ((BaseQuickAdapter) adapter).addData(list);
                    }
                    if (adapter instanceof BaseRecyclerAdapter) {
                        ((BaseRecyclerAdapter) adapter).addAll(list);
                    }
                } else {
                    ToastUtils.showMsg(AppUtils.getString(AppManager.getAppManager().currentActivity(), R.string.text_no_more_data));
                }
            }
        } else {
            if (page == firstPageCode) {
                if (adapter instanceof BaseQuickAdapter) {
                    ((BaseQuickAdapter) adapter).replaceData(new ArrayList());
                }
                if (adapter instanceof BaseRecyclerAdapter) {
                    ((BaseRecyclerAdapter) adapter).refresh(new ArrayList());
                }
            }
//            AppUtil.showToast(R.string.toast_nodata);
            LogX.e("###", "暂无数据");
        }
    }

    @Override
    public void complete() {
        moreRecyclerView.finishRefresh();
        moreRecyclerView.finishLoadMore(1);
    }

    public void refresh() {
        moreRecyclerView.autoRefresh();
    }
}
